/**
 * 
 */
package ro.dandanciu.webgallery.ui.galleries;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.log4j.Logger;

import ro.dandanciu.webgallery.domainmodel.Advert;
import ro.dandanciu.webgallery.domainmodel.Gallery;
import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.services.AdvertiseServices;
import ro.dandanciu.webgallery.services.UserServices;
import ro.dandanciu.webgallery.ui.general.WebGalleryAction;

/**
 * @author Dan
 *
 */
public class AdvertiseManagementAction extends WebGalleryAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4730041931341655982L;

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(AdvertiseManagementAction.class);

	private String mode;
	
	private AdvertiseServices advertiseServices;

	private UserServices userServices;
	
	long galleryId;
	
	private String text;
	
	private String[] selectedUsers;
	
	private List<User> users;

	private long id;
	
	private static final String ADVERTISE_MODE = "ADD";
	
	private static final String ADVERTISE_PAGE_MODE = "ADV";
	
	private static final String DELETE_MODE = "DEL";
	
	private static final String ADD_PAGE = "add_page";

	private static final String HOME_PAGE = "home_page";
	
	
	public AdvertiseManagementAction() {
		// TODO Auto-generated constructor stub
	}

	
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
		}

		String forward = null;
		if (ADVERTISE_PAGE_MODE.equals(mode)) {
			forward = executePrepare();
		} else if (ADVERTISE_MODE.equals(mode)) {
			forward = executeAdd();
		} else if (DELETE_MODE.equals(mode)){
			forward = executeDelete();
		} else {
			forward = setMessage("failure");
		}

		if (logger.isDebugEnabled()) {
			logger.debug("forward: " + forward);
			logger.debug("execute() - end");
		}
		return forward;
	}


	private String executeDelete() {
		advertiseServices.removeForUser(getSessionUser(), id);
		return HOME_PAGE;
	}


	private String executePrepare() {
		users = userServices.getUsers("");
		return ADD_PAGE;
	}

	private String executeAdd() {
		if (logger.isDebugEnabled()) {
			logger.debug("executeAdd() - start");
		}

		Advert advert = new Advert();
		Gallery gallery = galleryServices.getGalleryById(galleryId);
		Set<User> users = new TreeSet<User>();
		for (String userId : selectedUsers) {
			User user = userServices.getUserByUserId(userId);
			users.add(user);
		}
		advert.setAText(text);
		advert.setGallery(gallery);
		advert.setUsers(users);
		advert.setUpdater(getSessionUser().getUserId());

		advertiseServices.save(advert);
		String returnString = setMessage("advert.add.success");
		if (logger.isDebugEnabled()) {
			logger.debug("executeAdd() - end");
		}
		return returnString;
	}

	public long getGalleryId() {
		return galleryId;
	}

	public void setGalleryId(long galleryId) {
		this.galleryId = galleryId;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public AdvertiseServices getAdvertiseServices() {
		return advertiseServices;
	}

	public String getMode() {
		return mode;
	}
	public void setMode(String mode) {
		this.mode = mode;
	}
	public void setAdvertiseServices(AdvertiseServices advertiseServices) {
		this.advertiseServices = advertiseServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String[] getSelectedUsers() {
		return selectedUsers;
	}

	public void setSelectedUsers(String[] selectedUsers) {
		this.selectedUsers = selectedUsers;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
}
